# -*- coding: utf-8 -*-
#! /usr/bin/env python

import sys, re, os
import urllib2, urllib, cookielib
import logging
import xml.etree.ElementTree as ET

from config import *
from utils import *

logger = logging.getLogger('fer_fetcher')

#Post: post_id, thread_title, author, author_id, timestamp, forum_id, thread_id, unknown, thread_undertitle

_initial_dict = {'post_id': 0,
                       'post_text': '',
                       'thread_title': '',
                       'thread_undertitle': '',
                       'author': '',
                       'attach': '',
                       'author_id': 0,
                       'forum_id': 0,
                       'thread_id': 0,
                       'timestamp': 0,
                       }

class Post(object): 
    def __init__(self, _dict):
        self.__dict__.update(_initial_dict)
        for k in _initial_dict.keys():
            self.__dict__[k] = _dict[k]

    def __repr__(self):
        return u'<%s:%s>' % (self.post_id, self.timestamp)
        
    def __unicode__(self):
        return u'<%s:%s: %s>' % (self.post_id, self.author, self.post_text)
        

class Fer():
    def __init__(self):
        #if not os.path.exists(MAILDIR): os.mkdir(MAILDIR)
        self.data = urllib.urlencode(LOGIN_VALUES)
        self.cookies = cookielib.CookieJar()
        self.opener = urllib2.build_opener(
                urllib2.HTTPRedirectHandler(),
                urllib2.HTTPHandler(debuglevel=0),
                urllib2.HTTPSHandler(debuglevel=0),
                urllib2.HTTPCookieProcessor(self.cookies))
    
    def authorize(self):
        logger.info('Authorization start')
        response = self.opener.open(LOGIN_URL, self.data)
        the_page = response.read()
        http_headers = response.info()
        logger.info('Authorization done')

    def get_forums_list(self):
        logger.info('Get forums ...')
        forums_list = []
        response = self.opener.open(FORUMLIST_URL)
        data = response.read()
        # TODO: make xml parser, add error handler
        r = re.compile('<plist>(.*)</plist>')
        try: # TODO: make error catching
            for line in r.findall(data)[0].split('<|> <br>'):
                if line: forums_list.append(line.split('<|>'))
                #if line: print unicode(unescape(line), FORUM_ENCODING)
            r = re.compile('<bluetime>(.*)</bluetime>')
            bluetime = r.findall(data)[0]
            logger.info('Get forums done')
        except:
            return []
        if READ_PJ: forums_list.append(['164', u'ПЖ 2.0'.encode(FORUM_ENCODING), '0', bluetime, 'xxx', '0', '36'])
        return forums_list
        
    def get_threads_list(self, forum_id):
        logger.info('Get threads , forum_id = %s...' % forum_id)
        threads_list = []
        response = self.opener.open(THREADLIST_URL % forum_id)
        data = response.read()
        # TODO: make xml parser, add error handler
        r = re.compile('<plist>(.*)</plist>')
        if 'error' in data: return []
        for line in r.findall(data)[0].split('<br>'):
            if line: threads_list.append(line.split('<|>'))
        r = re.compile('<bluetime>(.*)</bluetime>')
        bluetime = r.findall(data)
        logger.info('Get threads done')
        return threads_list
        
    def get_posts_list(self, thread_id):
        logger.info('Get posts , thread_id = %s...' % thread_id)
        posts_list = []
        response = self.opener.open(POSTLIST_URL % thread_id)
        data = response.read()
        # TODO: make xml parser, add error handler
        r = re.compile('<pinfo>(.*)</pinfo>', flags = re.S)
        #print POSTLIST_URL % thread_id
        #print data
        t_title, f_id, unknown, unknown, t_answers, t_author, t_author_id, t_undertitle = r.findall(data)[0].split('<|>')
        r = re.compile('<plist>(.*)</plist>')
        for line in r.findall(data)[0].split('<br>'):
            if line: posts_list.append(line.split('<|>'))
        r = re.compile('<bluetime>(.*)</bluetime>')
        bluetime = r.findall(data)
        logger.info('Get posts done')
        return posts_list, t_title, f_id, unknown, unknown, t_answers, t_author, t_author_id, t_undertitle

    def get_posts(self, post_id, backlimit = 1):
        logger.info('Get posts, start post_id = %s...' % post_id)
        posts_list = []
        response = self.opener.open(POSTS_URL % (post_id, backlimit))
        data = response.read()
        # TODO: make xml parser, add error handler
        #print POSTS_URL % (post_id, backlimit)
        #print data
        r = re.compile('<xpost>(.*?)</xpost>', flags = re.S)
        for line in r.findall(data):
            r1 = re.compile('<hdr>(.*)</hdr>', flags = re.S)
            r2 = re.compile('<posttext>(.*)</posttext>', flags = re.S)
            r3 = re.compile('<attach>(.*)</attach>', flags = re.S)
            post_id, thread_title, author, author_id, timestamp, forum_id, thread_id, unknown, thread_undertitle = r1.findall(line)[0].split('<|>')
            post_text = r2.findall(line)[0]
            if r3.findall(line): attach = r3.findall(line)[0]
            post = Post(locals())
            posts_list.append(post)
        logger.info('Get posts done')
        return posts_list


if __name__ == "__main__":
    forum = Fer()
    forum.authorize()
    #print forum.get_forums_list()
    #print forum.get_threads_list('164')
    #print forum.get_posts_list('190953')
    print forum.get_posts('32194446', 1)

    for id, f_title in UL_THREADS.iteritems():
        if id !=36: continue
        f_data = ((0, f_title), )

    forums = forum.get_forums_list()

    for f in  forums:
        f_id, f_title, f_answers, f_bluetime, f_lastauthor, f_threads, f_parentid = f
        #print '---', unicode(unescape(f_title), FORUM_ENCODING)
        f_id = int(f_id)
        f_parentid = int(f_parentid)
        f_title = unicode(unescape(f_title), FORUM_ENCODING)
        if f_id not in (42, 91): continue
        
        for t in forum.get_threads_list(f_id):
            t_id, t_title, t_bluetime, t_open1, t_open2, t_answers, t_author, t_author_id, t_lastauthor, t_lastauthor_id, t_undertitle = t
            t_id = int(t_id)
            t_title = unicode(unescape(t_title), FORUM_ENCODING)
            
            #if t_id in disabled_ids: continue
            if t_undertitle: t_title+= "(%s)" % unicode(unescape(t_undertitle), FORUM_ENCODING)

            #print t_title

